package com.deyun.panelcabinet.utils;

import com.apkfuns.logutils.LogUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 字符串操作方法
 */
public class MyStringUtils {

    // byte数组转成字符串
    public static String byteToArray(byte[] data) {
        String result = "";
        for (int i = 0; i < data.length; i++) {
            result += Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3);
        }
        return result;
    }

    // 16进制字符串转2进制字符串
    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }

    // 2进制字符串转16进制字符串
    public static String binaryString2hexString(String bString) {
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuffer tmp = new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }


    // 解析关锁返回的字符串
    public static String ParseLockReturnInfo(String str) {
        // 0101030100006D8E
        // LogUtils.d("str length = " + str.length());
        if (str.length() != 16) {
            LogUtils.d("返回数据结构不正确");
            return null;
        }
        String lockInfo = str.substring(6, 12);
        String partOne = lockInfo.substring(0, 2);
        String partTwo = lockInfo.substring(2, 4);
        String partThree = lockInfo.substring(4, 6);
        StringBuffer result = new StringBuffer(hexString2binaryString(partOne)).reverse().append(new StringBuffer(hexString2binaryString(partTwo)).reverse()).append(new StringBuffer(hexString2binaryString(partThree)).reverse());

        return result.toString();
    }


    /**
     * @param str   传入的字符串
     * @param index 第几把锁的下标
     * @return
     */
    public static boolean getLockState(String str, int index) {
        char a = str.charAt(--index);
        if (a == '1') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 比较2个相同长度的字符串第一次出现不同内容的位置
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int comparisonGetNumberLock(String str1, String str2) {
        if (str1 == null || str2 == null) {
            return -1;
        }

        int temp = -1;

        char[] c1 = str1.toCharArray();
        char[] c2 = str2.toCharArray();
        for (int i = 0; i < c1.length; i++) {
            if (c1[i] != c2[i]) {
                temp = i;
                break;
            }
        }
        return temp;
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds   精确到秒的字符串
     * @return
     */
    public static String timeStamp2Date(String seconds) {
        if (seconds == null || seconds.isEmpty()) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(seconds)));
        return sd;
    }



    // 身份证正则校验
    public static boolean isIDNumber(String IDNumber) {
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression =
                "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
                        + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        boolean matches = IDNumber.matches(regularExpression);
        // 判断第18位校验值
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    // 前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    // 这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase()
                            .equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            } else {
                return false;
            }
        }
        return matches;
    }
}
